package com.ddc.study.feature.collect.website;

import com.ddc.study.entity.CollectWebsite;
import com.ddc.study.net.NetworkUtil;

import java.util.List;

/**
 * Created on 2018/6/7.
 *
 * @author lshun
 * @version 1.0
 */
class CollectWebsitePresenter extends CollectWebsiteContract.IPresenter {
    @Override
    void loadData() {
        Runnable runnable = () -> {
            final List<CollectWebsite> data = NetworkUtil.getCollectList();

            if (!isViewAttached()) return;
            getView().runOnUIThread(() -> {
                if (isViewAttached()) {
                    getView().showData(data);
                }
            });
        };

        mThreadPool.submit(runnable);
    }

    @Override
    void collectLink(final String name, final String link) {
        Runnable runnable = () -> {
            final CollectWebsite collectWebsite = NetworkUtil.collectLink(name, link);

            if (!isViewAttached()) return;
            getView().runOnUIThread(() -> {
                if (isViewAttached()) {
                    getView().showAddCollectLinkResult(collectWebsite);
                }
            });
        };

        mThreadPool.submit(runnable);
    }

    @Override
    void editCollectLink(final int id, final String name, final String link) {
        Runnable runnable = () -> {
            final CollectWebsite collectWebsite = NetworkUtil.editCollectLink(id, name, link);

            if (!isViewAttached()) return;
            getView().runOnUIThread(() -> {
                if (isViewAttached()) {
                    getView().showEditCollectLinkResult(collectWebsite);
                }
            });
        };

        mThreadPool.submit(runnable);
    }

    @Override
    void deleteLink(final int id) {
        Runnable runnable = () -> {
            final boolean result = NetworkUtil.deleteLink(id);

            if (!isViewAttached()) return;
            getView().runOnUIThread(() -> {
                if (isViewAttached()) {
                    getView().showDeleteLinkResult(result, id);
                }
            });
        };

        mThreadPool.submit(runnable);
    }
}
